package utils;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.Vector;

import sql.DBConnection;

public class SQLTypeMap 
{

	public static String getJavaType( String schema, String object, String column )
			throws Exception {
		String fullName = schema + '.' + object + '.' + column;
		DatabaseMetaData metaData = DBConnection.getInstance().getConnection().getMetaData();

		ResultSet rs = null;

		ResultSet rsColumns = null;
		rsColumns = metaData.getColumns(null, null, "dummy", null);
		while (rsColumns.next()) 
		{
			String columnName = rsColumns.getString("COLUMN_NAME");
			System.out.println("\ncolumn name=" + columnName);
			String columnType = rsColumns.getString("TYPE_NAME");
			System.out.println("type:" + columnType);
			int size = rsColumns.getInt("COLUMN_SIZE");
			System.out.println("size:" + size);
			int nullable = rsColumns.getInt("NULLABLE");
			if (nullable == DatabaseMetaData.columnNullable) {
				System.out.println("nullable true");
			} else {
				System.out.println("nullable false");
			}
			int position = rsColumns.getInt("ORDINAL_POSITION");
			System.out.println("position:" + position);

		}


		ResultSet columnMeta = metaData.getColumns( null, schema, object, column );
		String javaType = null;

		if( columnMeta.first() ) {
			int dataType = columnMeta.getInt( "DATA_TYPE" );

			String s = columnMeta.getString( "TYPE_NAME" );
			System.out.println(s + " " + dataType);

			javaType = SQLTypeMap.convertSqlToJava( dataType );
		}
		else {
			throw new Exception( "Unknown database column " + fullName + '.' );
		}

		return javaType;
	}

	/**
	 * Translates a data type from an integer (java.sql.Types value) to a string
	 * that represents the corresponding class.
	 * 
	 * @param type The java.sql.Types value to convert to a string representation.
	 * @return The class name that corresponds to the given java.sql.Types value,
	 *         or "java.lang.Object" if the type has no known mapping.
	 */
	public static String convertSqlToJava( int type ) {
		String result = "java.lang.Object";

		switch( type ) {
		case Types.CHAR:
		case Types.VARCHAR:
		case Types.LONGVARCHAR:
			result = "java.lang.String";
			break;

		case Types.NUMERIC:
		case Types.DECIMAL:
			result = "java.math.BigDecimal";
			break;

		case Types.BIT:
			result = "java.lang.Boolean";
			break;

		case Types.TINYINT:
			result = "java.lang.Byte";
			break;

		case Types.SMALLINT:
			result = "java.lang.Short";
			break;

		case Types.INTEGER:
			result = "java.lang.Integer";
			break;

		case Types.BIGINT:
			result = "java.lang.Long";
			break;

		case Types.REAL:
			result = "java.lang.Float";
			break;

		case Types.FLOAT:
		case Types.DOUBLE:
			result = "java.lang.Double";
			break;

		case Types.BINARY:
		case Types.VARBINARY:
		case Types.LONGVARBINARY:
			result = "java.lang.Byte[]";
			break;

		case Types.DATE:
			result = "java.sql.Date";
			break;

		case Types.TIME:
			result = "java.sql.Time";
			break;

		case Types.TIMESTAMP:
			result = "java.sql.Timestamp";
			break;
		}

		return result;
	}



	/**
	 * Translates a data type from an integer (java.sql.Types value) to a string
	 * that represents the corresponding class.
	 * 
	 * @param type The java.sql.Types value to convert to a string representation.
	 * @return The class name that corresponds to the given java.sql.Types value,
	 *         or "java.lang.Object" if the type has no known mapping.
	 */
	//DA FAre
	public static int convertJavaToSql( String javaType ) {
		int result = Types.VARCHAR;



		if (javaType.equals("java.lang.String"))
		{
			result = Types.VARCHAR;
		} 
		else if (javaType.equals("java.math.BigDecimal")) 
		{
			result = Types.DECIMAL;
		}
		else if (javaType.equals("java.lang.Boolean")) 
		{
			result = Types.BOOLEAN;
		}
		else if (javaType.equals("java.lang.Byte")) 
		{
			result = Types.TINYINT;
		}
		else if (javaType.equals("java.lang.Short")) 
		{
			result = Types.SMALLINT;
		}
		else if (javaType.equals("java.lang.Integer")) 
		{
			result = Types.INTEGER;
		}
		else if (javaType.equals("java.lang.Long")) 
		{
			result = Types.BIGINT;
		}
		else if (javaType.equals("java.lang.Float")) 
		{
			result = Types.REAL;
		}		
		else if (javaType.equals("java.lang.Double")) 
		{
			result = Types.DOUBLE;
		}		
		else if (javaType.equals("java.lang.Byte[]")) 
		{
			result = Types.VARBINARY;
		}		
		else if (javaType.equals("java.sql.Date")) 
		{
			result = Types.DATE;
		}		
		else if (javaType.equals("java.sql.Time")) 
		{
			result = Types.TIME;
		}		
		else if (javaType.equals("java.sql.Timestamp")) 
		{
			result = Types.TIMESTAMP;
		}		

		return result;
	}


	public static void main(String[] args)
	{
		String schema = "public";
		String table = "dummy";
		String column = "arrayint";

		try {
			String result = getJavaType(schema, table, column);
			System.out.println("Result: "+result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

}